package com.zhuozhou.jdyc.wight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.bean.PopupWindowBean;
import com.zhuozhou.jdyc.wight.popupWindow.adapter.ChildAdapter;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;
import com.zhuozhou.jdyc.wight.recyclerview.RecycleViewDivider;

import java.util.List;

/**
 * Created by 方舟 on 2017/10/18.
 * 单选弹框
 */

public class SingleSelectedDialog implements ChildAdapter.OnItemClickListener {

    public Context context;
    private List<PopupWindowBean> list;
    private ChildAdapter adapter;
    RecyclerView recyclerView;
    private Dialog customDialog;
    private CallBack callBack;

    public SingleSelectedDialog(Context context, List<PopupWindowBean> list) {
        this.context = context;
        this.list = list;
    }

    public Dialog create() {
        customDialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        recyclerView = new RecyclerView(context);
        recyclerView.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.HORIZONTAL, 1,
                ContextCompat.getColor(context, R.color.h_line_color)));
        recyclerView.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
        MyLayoutManager myLayoutManager = new MyLayoutManager(context);
        recyclerView.setLayoutManager(myLayoutManager);
        adapter = new ChildAdapter(context);
        adapter.setList(list);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        customDialog.setContentView(recyclerView);
        Window dialogWindow = customDialog.getWindow();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        if (wm != null) {
            wm.getDefaultDisplay().getMetrics(dm);
            dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                    dm.widthPixels * 2 / 5);
        } else {
            dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        dialogWindow.setGravity(Gravity.BOTTOM);
        customDialog.setCanceledOnTouchOutside(true);
        return customDialog;
    }

    @Override
    public void onItemClick(View view, int position) {
        if (customDialog.isShowing())
            customDialog.dismiss();
        TextView textView = view.findViewById(R.id.tv_parent_category_name);

        if (callBack != null) {
            callBack.callback(this, textView.getText().toString(), textView.getTag());
        }
    }

    public void setCallBackListener(CallBack callBack) {
        this.callBack = callBack;
    }

    public interface CallBack {
        void callback(SingleSelectedDialog dialog, String callBackString, Object tag);
    }

}
